import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Maze.Generator;
import NBT.ChunkEditor;
import NBT.MapChunk;


public class EditorPanel extends JPanel {

	final int maze_w=35;
	final int maze_h=35;

	public EditorPanel()
	{

		setBorder(BorderFactory.createTitledBorder("Generate maze: "));

		JButton backup=new JButton("Backup");
		JButton restore=new JButton("Restore");
		JButton save=new JButton("Store maze to chunk");
		JButton debug=new JButton("Debug");

		backup.addActionListener(new AbstractAction() {			
			public void actionPerformed(ActionEvent arg0) {						
				
				MapChunk chunk = Main.mainFrame.location_chooser.getSelectedChunk();				

				int cx=chunk.xPos;
				int cz=chunk.zPos;

				int cw=(int)Math.ceil((double)maze_w/16.0);
				int ch=(int)Math.ceil((double)maze_h/16.0);

				for(int x=0; x<cw; x++)
					for(int z=0; z<ch; z++)
					{
						chunk=Main.mainFrame.location_chooser.getChunk(cx+x, cz+z);
						if(chunk==null) continue;
						ChunkEditor editor=new ChunkEditor(chunk);
						editor.createBackup();
					}
			}
		});

		restore.addActionListener(new AbstractAction() {			
			public void actionPerformed(ActionEvent arg0) {

				if(JOptionPane.showConfirmDialog(null, "Are you sure?")!=JOptionPane.YES_OPTION)
				{
					return;
				}
				
				MapChunk chunk=Main.mainFrame.location_chooser.getSelectedChunk();

				int cx=chunk.xPos;
				int cz=chunk.zPos;

				
				int cw=(int)Math.ceil((double)maze_w/16.0);
				int ch=(int)Math.ceil((double)maze_h/16.0);

				File path=null;
				ChunkEditor chunk_editor=null;
				
				for(int x=0; x<cw; x++)
					for(int z=0; z<ch; z++)
					{
					
						chunk=Main.mainFrame.location_chooser.getChunk(cx+x, cz+z);
						if(chunk!=null) chunk_editor=new ChunkEditor(chunk);
						else 
						{
							path=Main.mainFrame.location_chooser.getChunkPath(cx+x, cz+z);
							if(path!=null) chunk_editor=new ChunkEditor(path);
						}
						if(chunk_editor!=null) chunk_editor.restoreBackup();
					}
			}
		});

		save.addActionListener(new AbstractAction() {			
			public void actionPerformed(ActionEvent arg0) {
				
				if(JOptionPane.showConfirmDialog(null, "Are you sure?")!=JOptionPane.YES_OPTION)
				{
					return;
				}

				Generator gen=new Generator(maze_w,maze_h);
				byte [] maze = gen.generate();
				int w = gen.getWidth();
				int h = gen.getHeight();

				int cw=(int)Math.ceil((double)maze_w/16.0);
				int ch=(int)Math.ceil((double)maze_h/16.0);


				MapChunk chunk = Main.mainFrame.location_chooser.getSelectedChunk();
				ChunkEditor chunk_editor=new ChunkEditor(chunk);
				
				chunk_editor.getLevelStatus();

				int cx=chunk.xPos;
				int cz=chunk.zPos;

				for(int x=0, sx=0; x<cw; x++, sx+=16)
					for(int z=0, sz=0; z<ch; z++, sz+=16)
					{
						chunk=Main.mainFrame.location_chooser.getChunk(cx+x, cz+z);

						if(chunk==null) continue;

						 chunk_editor.setChunk(chunk);

						try {
							chunk_editor.levelChunk();
							chunk_editor.makeMaze(maze, w, h, sx, sz, 0, 0);
							chunk_editor.save();
						} catch (Exception e) {
							Log.say("Cannot save file:\n"+e.getMessage());
						}
					}
			}
		});


		debug.addActionListener(new AbstractAction() {			
			public void actionPerformed(ActionEvent arg0) {					
				System.out.println(Main.mainFrame.location_chooser.getSelectedChunk().root.toString());
			}
		});

		add(backup);
		add(restore);
		add(save);
		//add(debug);
	}
}
